package com.jfinal.demo.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;


/**
 * dao层
 * @author tanghailan
 * @date 2020-12-17 15:11
 */
public class ShoolInfoDao extends Model<ShoolInfoDao>{
    public static final ShoolInfoDao dao = new ShoolInfoDao().dao();
    public static final ShoolInfoDao crud = new ShoolInfoDao();

    //修改学校信息
    public static Boolean updateShoolInfo(Integer shId,
                                          String shCity,
                                          String shShool){
        Record record = new Record().set("sh_id",shId).
                set("sh_city",shCity).
                set("sh_shool",shShool);
        return Db.update("shool_info","sh_id",record);
    }

    //新增学校信息
    public static Boolean addShoolInfo(String shCity,
                                       String shShool){
        Record record = new Record().
                set("sh_city",shCity).
                set("sh_shool",shShool);
        return Db.save("shool_info", record);
    }

    //根据id删除信息
    public static Integer deleteShoolInfo(Long id){
        return Db.delete("delete from `jjw`.`shool_info` where sh_id = " + id);
    }

    //根据id获取信息
    public static ShoolInfoDao getById(Long id){
        return ShoolInfoDao.dao.findFirst("select * from shool_info where sh_id ="+id);
    }

    //获取分页学校列表
    public static Page<ShoolInfo> getPageShoolInfoList(int pageNumber,int pageSize){
        Page<ShoolInfoDao> paginate = ShoolInfoDao.dao.paginate(pageNumber, pageSize, "select *", "from `jjw`.`shool_info` order by sh_id desc");
        List<ShoolInfoDao> daoList = paginate.getList();
        List<ShoolInfo> infoList = new ArrayList<>();
        for (ShoolInfoDao shoolInfoDao : daoList) {
            ShoolInfo info = new ShoolInfo();
            info.setSh_city(shoolInfoDao.getStr("sh_city"));
            info.setSh_id(shoolInfoDao.getInt("sh_id"));
            info.setSh_shool(shoolInfoDao.getStr("sh_shool"));
//            BeanUtils.copyProperties(shoolInfoDao,info);
            infoList.add(info);
        }
        Page<ShoolInfo> infoPage = new Page<>();
        infoPage.setList(infoList);
        infoPage.setTotalRow(paginate.getTotalRow());
        infoPage.setTotalPage(paginate.getTotalPage());
        infoPage.setPageNumber(paginate.getPageNumber());
        infoPage.setPageSize(paginate.getPageSize());
        return infoPage;
    }
}
